﻿using System.IO;
using System.Linq;
using Framework.Core.Io;
using Framework.Core.Process;
using Framework.Core.Process.Tasks;
using Rhino.Mocks;
using Xunit;

namespace Framework.Tests.Core.Process.Tasks
{
    public class TaskListTest
    {
        [Fact]
        public void Should_validate_command()
        {
            var mockRepository = new MockRepository();
            var processRunnerMock = mockRepository.StrictMock<IProcessRunner>();
            var taskList = new TaskList();

            Assert.False(taskList.IsNative);
            Assert.Empty(taskList.Arguments);
            const string path = @"PsArgs.exe";

            Assert.Equal(path, taskList.FileName);
            Assert.Equal(Path.GetFileNameWithoutExtension(path), taskList.Name);

            Assert.True(File.Exists(path));

            var output = new[]
            {
                GetOuputProcess("skipHeader", 0), 
                GetOuputProcess("devenv", 4), 
                GetOuputService("svcHello", 10)
            };

            processRunnerMock
                .Expect(x => x.Start(taskList))
                .Return(new ProcessResult(0, string.Join(System.Environment.NewLine, output), string.Empty, false));

            mockRepository.ReplayAll();
            var result = taskList.Execute(processRunnerMock).ToList();
            mockRepository.VerifyAll();

            Assert.NotEmpty(result);
            Assert.IsType<WindowsService>(result.Last());
            Assert.IsType<Framework.Core.Process.Process>(result.First());
        }

        private static string GetOuputProcess(string service, int pid)
        {
            return string.Format(@"{0}		{1}	{2}	C:\Windows\system32\mmc.exe	C:\Windows\system32\mmc.exe 3121456", Path.GetFileNameWithoutExtension(service), pid, service);
        }

        private static string GetOuputService(string service, int pid)
        {
            return string.Format(@"{0}	{0}	{1}	{2}	C:\Windows\system32\mmc.exe	C:\Windows\system32\mmc.exe 3121456", Path.GetFileNameWithoutExtension(service), pid, service);
        }
    }
}
